//
//  UpdateUserTelephone.swift
//  d11
//
//  Created by rhonin on 2018/10/30.
//  Copyright © 2018年 Tech_001. All rights reserved.
//

import UIKit
import SwiftyJSON
class UpdateUserTelephone: UpdateRootViewController {
    
    // 距离父视图顶部间距
    @IBOutlet weak var bottomViewTopConstraint: NSLayoutConstraint!
    
    @IBOutlet weak var buttonSingnin: UIButton!
    var timer = TimerHander()
    
    @IBOutlet weak var coutLab: UILabel!
    @IBOutlet weak var viewCode: UIView!
    @IBOutlet weak var codeText: UITextField!
    @IBOutlet weak var getCodeBUtton: UIButton!
    @IBOutlet weak var username: UITextField!
    @IBOutlet weak var viewCome: UIView!
    var isCancel = false
    override func viewDidLoad() {
        super.viewDidLoad()
        self.viewCome.layer.borderColor = RGBAlpa(219,219,219,1).cgColor
        self.viewCode.layer.borderColor = RGBAlpa(219,219,219,1).cgColor
        // Do any additional setup after loading the view.
        let layerGradient = CAGradientLayer()
        layerGradient.colors = [
            UIColor.init(red: 1, green: 0.78, blue: 0.45, alpha: 1).cgColor,
            UIColor.init(red: 0.99, green: 0.57, blue: 0.33, alpha: 1).cgColor
        ]
        layerGradient.frame = CGRect(x: 0, y: 0, width: buttonSingnin.frame.width, height: buttonSingnin.frame.height+50)
        buttonSingnin.layer.addSublayer(layerGradient)
        buttonSingnin.layer.cornerRadius = 5

        self.title = "修改手机号码"
        self.getCodeBUtton.clipsToBounds = true
        self.getCodeBUtton.layer.cornerRadius = 6
        self.getCodeBUtton.layer.borderColor = RGBAlpa(255,198,116,1).cgColor
        self.getCodeBUtton.layer.borderWidth = 0.5
        
        self.bottomViewTopConstraint.constant = kNavigationBarHeight
        // 设置导航栏默认返回按钮
        self.leftButtonActionWithImg("icon_default_back")
    }
    
    @IBAction func updateUserTel(){
        if username.text!.count == 11 {
            weak var this = self;
            let progress = MBProgressHUD.showMessageNoHiden("", to: self.view)
            RequestHelper.post(atPath: "/clientsAPIs/SaveMobile", withData: ["mobile":username.text!,"code":codeText.text!], withCompletion: { data in
                let json = JSON(data)
                let code = json["success"].boolValue
                if code {
                    DispatchQueue.main.async {
                        progress.removeFromSuperview()
                        UIHelper.alertSimpleMessage(about: "更改手机号码成功", withDescription: "", in: self)
                        this?.perform(#selector(this?.popcont), with: nil, afterDelay: 0.5)
                    }
                }else{
                    DispatchQueue.main.async {
                         progress.removeFromSuperview()
                        UIHelper.alertSimpleMessage(about: "更改失败", withDescription: json["msg"].stringValue, in: self)
                        this?.perform(#selector(this?.popcont), with: nil, afterDelay: 0.5)
                        
                    }
                }
            },withError: {_ in
                 UIHelper.alertSimpleMessage(about: "服务器异常，\(getErrorString())", withDescription: "", in: self)
            })
        }else{
            UIHelper.alertSimpleMessage(about: "", withDescription: "填写正确手机号码", in: self)
        }
        
    }
   
    @IBAction func getCode(_ sender: Any) {
        if username.text!.count != 11{
            UIHelper.alertSimpleMessage(about: "", withDescription: "填写正确手机号码", in: self)
            return
        }
        self.startTimer()
        RequestHelper.post(atPath: "/clientsAPIs/GetMobileToken", withData: ["mobile":username.text!], withCompletion: { data in
            let json = JSON(data)
            let code = json["success"].boolValue
            if code  {
                DispatchQueue.main.async {
                    UIHelper.alertSimpleMessage(about: "获取短信验证码成功", withDescription: "请查看您的短信", in: self)
                    
                }
            }else{
                DispatchQueue.main.async {
                    
                    UIHelper.alertSimpleMessage(about: "获取短信验证码失败", withDescription: json["msg"].stringValue, in: self)
                    
                }
            }
        },withError: {_ in 
             UIHelper.alertSimpleMessage(about: "服务器异常，\(getErrorString())", withDescription: "", in: self)
        })
    }
    
    func  startTimer(){
        self.timer.startTimer(second: 120) { (count) in
            if count > 0{
                self.coutLab.text =  "\(count)s"
                self.getCodeBUtton.isUserInteractionEnabled = false
            }else{
                self.coutLab.text = "获取验证码"
                 self.getCodeBUtton.isUserInteractionEnabled = true
            }
        }
    }
    @objc func popcont()  {
        self.navigationController?.popViewController(animated: true)
    }

}
